// 游戏板尺寸
const BOARD_SIZE = 8;
// 元素类型数量
const ELEMENT_TYPES = 4;

// 获取游戏板元素
const gameBoard = document.getElementById('game-board');

// 初始化游戏板数组
let board = [];
for (let i = 0; i < BOARD_SIZE; i++) {
    board[i] = new Array(BOARD_SIZE).fill(0);
}

// 生成随机元素类型
function getRandomElementType() {
    return Math.floor(Math.random() * ELEMENT_TYPES);
}

// 初始化游戏板
function initBoard() {
    for (let i = 0; i < BOARD_SIZE; i++) {
        for (let j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = getRandomElementType();
        }
    }
    // 检查并消除初始匹配
    while (checkAndRemoveMatches()) {
        fillEmptyCells();
    }
    updateBoard();
}

// 更新游戏板显示
function updateBoard() {
    gameBoard.innerHTML = '';
    for (let i = 0; i < BOARD_SIZE; i++) {
        for (let j = 0; j < BOARD_SIZE; j++) {
            const cell = document.createElement('div');
            cell.classList.add('cell');
            switch (board[i][j]) {
                case 0:
                    cell.classList.add('red');
                    break;
                case 1:
                    cell.classList.add('blue');
                    break;
                case 2:
                    cell.classList.add('green');
                    break;
                case 3:
                    cell.classList.add('yellow');
                    break;
            }
            cell.dataset.row = i;
            cell.dataset.col = j;
            cell.addEventListener('click', handleCellClick);
            gameBoard.appendChild(cell);
        }
    }
}

// 检查并消除匹配元素
function checkAndRemoveMatches() {
    let hasMatch = false;
    // 检查水平匹配
    for (let i = 0; i < BOARD_SIZE; i++) {
        for (let j = 0; j < BOARD_SIZE - 2; j++) {
            if (board[i][j] === board[i][j + 1] && board[i][j] === board[i][j + 2]) {
                board[i][j] = -1;
                board[i][j + 1] = -1;
                board[i][j + 2] = -1;
                hasMatch = true;
            }
        }
    }
    // 检查垂直匹配
    for (let j = 0; j < BOARD_SIZE; j++) {
        for (let i = 0; i < BOARD_SIZE - 2; i++) {
            if (board[i][j] === board[i + 1][j] && board[i][j] === board[i + 2][j]) {
                board[i][j] = -1;
                board[i + 1][j] = -1;
                board[i + 2][j] = -1;
                hasMatch = true;
            }
        }
    }
    return hasMatch;
}

// 填充空单元格
function fillEmptyCells() {
    for (let j = 0; j < BOARD_SIZE; j++) {
        let emptyIndex = BOARD_SIZE - 1;
        for (let i = BOARD_SIZE - 1; i >= 0; i--) {
            if (board[i][j]!== -1) {
                board[emptyIndex][j] = board[i][j];
                if (emptyIndex!== i) {
                    board[i][j] = -1;
                }
                emptyIndex--;
            }
        }
        // 填充顶部空单元格
        for (let i = emptyIndex; i >= 0; i--) {
            board[i][j] = getRandomElementType();
        }
    }
}

// 处理单元格点击事件
let selectedCell = null;
function handleCellClick() {
    const row = parseInt(this.dataset.row);
    const col = parseInt(this.dataset.col);
    if (selectedCell === null) {
        selectedCell = { row, col };
    } else {
        const { row: prevRow, col: prevCol } = selectedCell;
        // 检查是否相邻
        if ((Math.abs(row - prevRow) === 1 && col === prevCol) || (Math.abs(col - prevCol) === 1 && row === prevRow)) {
            // 交换元素
            [board[row][col], board[prevRow][prevCol]] = [board[prevRow][prevCol], board[row][col]];
            if (checkAndRemoveMatches()) {
                fillEmptyCells();
            } else {
                // 没有匹配，交换回来
                [board[row][col], board[prevRow][prevCol]] = [board[prevRow][prevCol], board[row][col]];
            }
            updateBoard();
        }
        selectedCell = null;
    }
}

// 初始化游戏
initBoard();